package com.loongtech.bi.manager.player;

import com.loongtech.bi.entity.player.EntityDau;
import com.loongtech.bi.entity.player.EntityDauTag;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DauManager extends TypeNewManager<EntityDau> {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 根据开始结束时间，返回所有在这两个时间之间的单日DAU对象
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<EntityDau> getDauByDate(Date dateStart, Date dateEnd) {
		return findByQuery("from EntityDau where 1=1 and startday>=:dayStart and endday<=:dayEnd and startday=endday order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
	}

    /**
     * 根据开始结束时间，返回所有在这两个时间之间的单日DAU对象
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityDau> getDauByDate2(Date dateStart, Date dateEnd) {
        return findByQuery("from EntityDau where 1=1 and startday>=:dayStart and endday<=:dayEnd and startday=endday and serverid='0' order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
    }

    /**
     * 根据开始结束时间服务器id，返回所有在这两个时间之间的单日DAU对象
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityDauTag> getDauByDateByTag(String serverid, Date dateStart, Date dateEnd) {
        return findByQuery("from EntityDauTag where 1=1 and serverid = ? and startday>=? and endday<=? and startday=endday order by startday ", serverid, Time.getDateStr(dateStart), Time.getDateStr(dateEnd));
    }

	/**
	 * 根据开始结束时间，返回所有在这两个时间之间的所有DAU对象
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<EntityDau> getAllDauByDate(Date dateStart, Date dateEnd) {
		return findByQuery("from EntityDau where 1=1 and startday=:dayStart and endday=:dayEnd order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
	}

    /**
     * 根据开始结束时间，返回所有在这两个时间之间的所有DAU对象
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityDau> getAllDauByDate2(Date dateStart, Date dateEnd) {
        return findByQuery("from EntityDau where 1=1 and startday=:dayStart and endday=:dayEnd and serverid='0' order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
    }

    /**
     * 根据开始结束时间服务器id，返回所有在这两个时间之间的所有DAU对象
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityDauTag> getAllDauByDateByTag(String serverid, Date dateStart, Date dateEnd) {
        return findByQuery("from EntityDauTag where 1=1 and serverid = ? and startday=? and endday=? order by startday ", serverid, Time.getDateStr(dateStart), Time.getDateStr(dateEnd));
    }

	/**
	 * 根据开始结束时间，返回所有在这两个时间之间的所有DAU对象
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<EntityDau> getAllDauByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDau where 1=1 and startday=:dayStart and endday=:dayEnd  and platformid=:platformid and channelid=:channelid and serverid=:serverid order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	/**
	 * 根据开始结束时间和平台渠道区服条件，返回所有在这两个时间之间的单日DAU对象
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<EntityDau> getDauByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDau where 1=1 and startday>=:dayStart and endday<=:dayEnd and startday=endday and platformid=:platformid and channelid=:channelid and serverid=:serverid order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}

    public List<Object[]> getNewUserByAllKind(Date dateStart, Date dateEnd,String queryPlatform, String queryChannel, String queryServer) {
        return findByQuery("select date_format(startday,'%Y-%m-%d'),dau from EntityDau where startday>=:dayStart and endday<=:dayEnd and startday=endday and platformid=:platformid and channelid=:channelid and serverid=:serverid order by startday ", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
    }

	/**
	 * 根据platformid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getDauGroupByPlatform(Date dateStart, Date dateEnd) {
		return findByQuery("select platformid,sum(dau) from EntityDau where startday=:dayStart and endday=:dayEnd and platformid != '0' and dau is not null group by platformid", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
	}
	
	/**
	 * 根据channelid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getDauGroupByChannel(Date dateStart, Date dateEnd) {
		return findByQuery("select channelid,sum(dau) from EntityDau where startday=:dayStart and endday=:dayEnd and channelid != '0' and dau is not null group by channelid", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
	}
	
	/**
	 * 根据serverid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getDauGroupByServer(Date dateStart, Date dateEnd) {
		return findByQuery("select serverid,sum(dau) from EntityDau where startday=:dayStart and endday=:dayEnd and serverid != '0' and dau is not null group by serverid", "dayStart", Time.getDateStr(dateStart), "dayEnd", Time.getDateStr(dateEnd));
	}
}
